package net.kts.util.sqlite;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class MoveDatabase {
	
	private Context context = null;
	private String pakageName = null;//"net.kts.subskill.toilet";
	private String dbFileName = null;//"kts.db";
	private String assertPath = null;//"koreaTravelStory.db3";
	
	public MoveDatabase(Context context, String pakageName, String createDbFileName, String assertPath) {
		this.context = context;
		this.pakageName = pakageName;
		this.assertPath = assertPath;
		this.dbFileName = createDbFileName;
	}
	
	
	public void assertToData() {
		File root  = new File("/data/data/" + pakageName +"/databases");

		if(!root.isDirectory()) {
			root.mkdirs();
		}

		File outfile = new File(root, dbFileName);


		AssetManager assetManager = this.context.getResources().getAssets();
		InputStream is;
		try {
			is = assetManager.open(assertPath, AssetManager.ACCESS_BUFFER);
			
			long filesize = is.available();

			// 현재  assert 포럳에 있는 db 파일이 설치도니 파일보다 용량이 작을때! 다시 설치한다!
//			if(outfile.length() < filesize){
				byte[] tempdata = new byte[(int) filesize];
				is.read(tempdata);
				is.close();
				outfile.createNewFile();
				FileOutputStream fo = new FileOutputStream(outfile);
				fo.write(tempdata);
				fo.close();
//			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d("moveDatabase", e.getMessage());
		}
		
		

	}
}
